package com.ruoyi.framework.aspectj;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampCarList;
import com.ruoyi.eamp.domain.EampCarLogs;
import com.ruoyi.eamp.domain.EampCarUse;
import com.ruoyi.eamp.service.IEampCarListService;
import com.ruoyi.eamp.service.IEampCarLogsService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Aspect
@Component
public class UselogsAspect {


    @Autowired
    private IEampCarLogsService eampCarLogsService;

    @Autowired
    private IEampCarListService eampCarListService;

    @Pointcut("@annotation(com.ruoyi.common.annotation.UseLogs)")
    public void userlogs() {

    }

    @Around("userlogs()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String name = ((MethodSignature) joinPoint.getSignature()).getMethod().getName();
        Object result = joinPoint.proceed();
        // 新增添加
        Long carId = null;
        Long carOwners = null;
        Long carCreatePeople = null;
        Long useId=null;
        Date date = new Date();
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof EampCarList) {
                carId = ((EampCarList) arg).getCarId();
                carOwners = ((EampCarList) arg).getCarOwners();
                carCreatePeople = ((EampCarList) arg).getCarCreatePeople();
            }
            if (arg instanceof EampCarUse) {
                carId = ((EampCarUse) arg).getCarId();
                useId = ((EampCarUse) arg).getUseId();
                carOwners = eampCarListService.selectEampCarListByCarId(carId).getCarOwners();
                carCreatePeople = ((EampCarUse) arg).getUseApplicant();
            }
        }

        EampCarLogs eampCarLogs = new EampCarLogs(null,1L,carOwners,carId,carCreatePeople,date,name,useId,0L);
        eampCarLogsService.insertEampCarLogs(eampCarLogs);


        return result;
    }


}
